#coding:utf8
#
# Author: CMGS
# Created on 2010-8-24
#

from gevent import Timeout
from gevent import Greenlet

from utils import feedparser as _feed
from common import config as _conf

class Crawler(Greenlet):
    
    def __init__(self, url, timeout = _conf.CRAWLER_TIMEOUT, etag = None, modified = None):
        Greenlet.__init__(self)
        self._url = url
        self._etag = etag
        self._modified = modified
        self._timeout = timeout

    def _run(self):
        with Timeout(self._timeout, False):
            ret = _feed.parse(self._url, etag = self._etag, modified = self._modified)
        return ret
    
    def __str__(self):
        return 'Crawl %s' % self.url

if __name__ == '__main__':
    url = u'http://www.verycd.com/sto/feed'
    c = Crawler(url, 0.1)
    print c.parse()
